<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Model;
use Think\Model;
class AuthModel extends Model{
//    实现权限添加
    public function saveData($data){
//      1.根据已有$data(name,pid,controller,action)数据生成数据
        
        $newid = $this->add($data);//添加创建新纪录成功返回主键id值
//      2.制作auth_path
//            a.顶级权限auth_path------>新纪录主键id值
        if($data['auth_pid']==0){
            $path = $newid;
        }else{
//           b.非顶级权限 根据pid获得父级权限的auth_id,与新纪录authidzuhe
//            父级全路径-新纪录的主键id值
            $pinfo = $this->find($data['auth_pid']);
            $path = $pinfo['auth_path']."-".$newid;
        }
//      3.制作auth_level,是全路径的“-”数量；计算一个字符串中目标出现的次数substr_count();
        $level = substr_count($path,'-');
        $sql = "update auth set auth_path='$path',auth_level='$level' where auth_id='$newid'";
        return $this->execute($sql);
    }
}
